package cn.wolfcode.rbac.web.controller;

import cn.wolfcode.rbac.domain.Customer;
import cn.wolfcode.rbac.domain.CustomerTransfer;
import cn.wolfcode.rbac.query.QueryObject;
import cn.wolfcode.rbac.service.ICustomerService;
import cn.wolfcode.rbac.service.ICustomerTransferService;
import cn.wolfcode.rbac.util.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/customerTransfer")
public class CustomerTransferController {
    @Autowired
    private ICustomerTransferService service;
    @Autowired
    private ICustomerService customerService;


    @RequestMapping("/list")
    public String list(@ModelAttribute("qo") QueryObject qo, Model model){
        model.addAttribute("result",this.service.query(qo));
        return "customerTransfer/list";
    }


    @RequestMapping("/saveOrUpdate")
    @ResponseBody
    public JsonResult saveOrUpdate(CustomerTransfer cth){
        Customer customer = new Customer();
        customer.setId(cth.getCustomerId());
        customer.setSellerId(cth.getNewSellerId());
        this.customerService.updateSeller(customer);
        this.service.saveOrUpdate(cth);
        return new JsonResult();
    }

    @Autowired
    private ICustomerService csService;

    @RequestMapping("/absorb")
    @ResponseBody
    public JsonResult absorb(CustomerTransfer ctf){
        Customer customer = new Customer();
        customer.setId(ctf.getCustomerId());
        customer.setStatus(5);
        this.csService.updateStatus(customer);
        ctf.setNewSellerId(ctf.getOldSellerId());
        this.service.saveOrUpdate(ctf);
        return new JsonResult();
    }
}
